package top.wyloop.util.password;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;

/**
 * @author Wyatt6
 * @date 2025/5/22
 */
public class AESUtil {
    private static final String ALGORITHM = "AES";

    /**
     * 加密方法
     *
     * @param plaintext 明文
     * @param keyString 密钥
     * @return 密文
     */
    public static String encrypt(String plaintext, String keyString) {
        try {
            // 将用户输入的密钥字符串生成128位的密钥
            KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);        // 声明密钥生成器：使用AES算法
            // 这里替换下面那个init，解决mac/linux环境报错：
            // macGiven final block not properly padded. Such issues can arise if a bad key is used during decryption.
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(keyString.getBytes());
            kgen.init(128, random); // 初始化密钥生成器：128位，输入的密钥串作为种子
            //kgen.init(128, new SecureRandom(keyString.getBytes()));
            SecretKey secretKey = kgen.generateKey();                       // 生成密钥

            // 加密
            byte[] encodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(encodeFormat, ALGORITHM); // 转换为AES专用密钥模版
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");              // 创建密码器
            cipher.init(Cipher.ENCRYPT_MODE, key);                      // 初始化密码器：加密模式，密钥
            return new Base64().encodeToString(cipher.doFinal(plaintext.getBytes()));    // byte[] --> String
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 解密方法
     *
     * @param ciphertext 密文
     * @param keyString  密钥
     * @return 明文
     */
    public static String decrypt(String ciphertext, String keyString) {
        try {
            // 将用户输入的密钥字符串生成128位的密钥
            KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
            // 这里替换下面那个init，解决mac/linux环境报错：
            // macGiven final block not properly padded. Such issues can arise if a bad key is used during decryption.
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(keyString.getBytes());
            kgen.init(128, random); // 初始化密钥生成器：128位，输入的密钥串作为种子
            //kgen.init(128, new SecureRandom(keyString.getBytes()));
            SecretKey secretKey = kgen.generateKey();

            // 解密
            byte[] encodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(encodeFormat, ALGORITHM); // 转换为AES专用密钥模版
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");              // 创建密码器
            cipher.init(Cipher.DECRYPT_MODE, key);                      // 初始化密码器：解密模式，密钥
            return new String(cipher.doFinal(Base64.decodeBase64(ciphertext)));    // String --> byte[]
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
